<html>
<head><meta charset="utf-8"><title>Validating Trait&#x27;s Associated Constants · project-const-generics · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/index.html">project-const-generics</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/Validating.20Trait&#x27;s.20Associated.20Constants.html">Validating Trait&#x27;s Associated Constants</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="238020704"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/Validating%20Trait%27s%20Associated%20Constants/near/238020704" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/Validating.20Trait&#x27;s.20Associated.20Constants.html#238020704">(May 09 2021 at 08:54)</a>:</h4>
<p><span class="user-mention" data-user-id="216206">@lcnr</span> I'm not sure if this is more of a const generic issue or const eval issue, so feel free to move it if it suits one more than the other.</p>
<p>Continuing from the discussion at the last meeting, I've been experimenting to try and find a way to validate that associated constants of a trait are correct. However I can't seem to find a way to get past <code>constant expression depends on a generic parameter</code>. Is this a hard limit with the current implementation at the moment?</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#![feature(const_generics, const_evaluable_checked)]</span><span class="w"></span>
<span class="cp">#![allow(incomplete_features)]</span><span class="w"></span>

<span class="sd">/// A trait representing an abstract conditional.</span>
<span class="cp">#[doc(hidden)]</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">trait</span><span class="w"> </span><span class="n">Condition</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">C</span>: <span class="kt">bool</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">trait</span><span class="w"> </span><span class="n">IsTrue</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">IsTrue</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="k">dyn</span><span class="w"> </span><span class="n">Condition</span><span class="o">&lt;</span><span class="kc">true</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="sd">/// A trait representing any type that can represented in ASN.1.</span>
<span class="k">pub</span><span class="w"> </span><span class="k">trait</span><span class="w"> </span><span class="n">AsnType</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="sd">/// The associated tag for the type.</span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="n">TAG</span>: <span class="nc">Tag</span><span class="p">;</span><span class="w"></span>

<span class="w">    </span><span class="sd">/// The tags associated with all of the members or variants, if you're</span>
<span class="w">    </span><span class="sd">/// implementing a `SEQUENCE`, `SET`, or `SEQUENCE` type.</span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="n">MEMBER_TAGS</span>: <span class="kp">&amp;</span><span class="o">'</span><span class="nb">static</span> <span class="p">[</span><span class="n">Tag</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="p">[];</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="sd">/// Marker trait representing a valid ASN.1 type.</span>
<span class="k">pub</span><span class="w"> </span><span class="k">trait</span><span class="w"> </span><span class="n">ValidAsnType</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">A</span>: <span class="nc">AsnType</span><span class="o">&gt;</span><span class="w"> </span><span class="n">ValidAsnType</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">A</span><span class="w"></span>
<span class="w">    </span><span class="k">where</span><span class="w"> </span><span class="k">dyn</span><span class="w"> </span><span class="n">Condition</span><span class="o">&lt;</span><span class="p">{</span><span class="w"> </span><span class="n">valid_asntype</span><span class="p">(</span><span class="n">A</span>::<span class="n">TAG</span><span class="p">,</span><span class="w"> </span><span class="n">A</span>::<span class="n">MEMBER_TAGS</span><span class="p">)</span><span class="w"> </span><span class="p">}</span><span class="o">&gt;</span>: <span class="nc">IsTrue</span><span class="w"></span>
<span class="p">{}</span><span class="w"></span>

<span class="k">const</span><span class="w"> </span><span class="k">fn</span> <span class="nf">valid_asntype</span><span class="p">(</span><span class="n">TAG</span>: <span class="nc">Tag</span><span class="p">,</span><span class="w"> </span><span class="n">MEMBER_TAGS</span>: <span class="kp">&amp;</span><span class="o">'</span><span class="nb">static</span> <span class="p">[</span><span class="n">Tag</span><span class="p">])</span><span class="w"> </span>-&gt; <span class="kt">bool</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="o">!</span><span class="n">TAG</span><span class="p">.</span><span class="n">const_eq</span><span class="p">(</span><span class="n">Tag</span>::<span class="n">EOC</span><span class="p">)</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">Tag</span>::<span class="n">is_distinct_set</span><span class="p">(</span><span class="n">MEMBER_TAGS</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="cp">#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">enum</span> <span class="nc">Class</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">Universal</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">Application</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">Context</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">Private</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="sd">/// An abstract representation of the tag octets used in BER, CER, and</span>
<span class="sd">/// DER to identify .</span>
<span class="cp">#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord)]</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">struct</span> <span class="nc">Tag</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="n">class</span>: <span class="nc">Class</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="n">value</span>: <span class="kt">u32</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">Tag</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">EOC</span>: <span class="nc">Tag</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Tag</span>::<span class="n">new</span><span class="p">(</span><span class="n">Class</span>::<span class="n">Universal</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span><span class="w"></span>

<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="k">fn</span> <span class="nf">new</span><span class="p">(</span><span class="n">class</span>: <span class="nc">Class</span><span class="p">,</span><span class="w"> </span><span class="n">value</span>: <span class="kt">u32</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="bp">Self</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">class</span><span class="p">,</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>

<span class="w">    </span><span class="sd">/// Checks that a slice of tags is a distinct set in a way that is `const`</span>
<span class="w">    </span><span class="sd">/// compatible.</span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="k">fn</span> <span class="nf">is_distinct_set</span><span class="p">(</span><span class="n">set</span>: <span class="kp">&amp;</span><span class="p">[</span><span class="bp">Self</span><span class="p">])</span><span class="w"> </span>-&gt; <span class="kt">bool</span> <span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="k">while</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">set</span><span class="p">.</span><span class="n">len</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="kd">let</span><span class="w"> </span><span class="n">needle</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="n">set</span><span class="p">[</span><span class="n">index</span><span class="p">];</span><span class="w"></span>
<span class="w">            </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">after</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"></span>
<span class="w">            </span><span class="k">while</span><span class="w"> </span><span class="n">after</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">set</span><span class="p">.</span><span class="n">len</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="k">if</span><span class="w"> </span><span class="n">needle</span><span class="p">.</span><span class="n">const_eq</span><span class="p">(</span><span class="n">set</span><span class="p">[</span><span class="n">after</span><span class="p">])</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="o">!</span><span class="n">needle</span><span class="p">.</span><span class="n">const_eq</span><span class="p">(</span><span class="n">Tag</span>::<span class="n">EOC</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                    </span><span class="k">return</span><span class="w"> </span><span class="kc">false</span><span class="p">;</span><span class="w"></span>
<span class="w">                </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                    </span><span class="n">after</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"></span>
<span class="w">                </span><span class="p">}</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>

<span class="w">            </span><span class="n">index</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>

<span class="w">        </span><span class="kc">true</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>

<span class="w">    </span><span class="k">pub</span><span class="p">(</span><span class="k">crate</span><span class="p">)</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="k">fn</span> <span class="nf">const_eq</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">rhs</span>: <span class="nc">Self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">bool</span> <span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="bp">self</span><span class="p">.</span><span class="n">class</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">u8</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">rhs</span><span class="p">.</span><span class="n">class</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">u8</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">value</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">rhs</span><span class="p">.</span><span class="n">value</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<div class="codehilite"><pre><span></span><code>error: constant expression depends on a generic parameter
  --&gt; src/lib.rs:27:69
   |
27 |     where dyn Condition&lt;{ valid_asntype(A::TAG, A::MEMBER_TAGS) }&gt;: IsTrue
   |                                                                     ^^^^^^
   |
   = note: this may fail depending on what value the parameter takes
</code></pre></div>



<a name="238021221"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/Validating%20Trait%27s%20Associated%20Constants/near/238021221" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Boxy [she/her] <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/Validating.20Trait&#x27;s.20Associated.20Constants.html#238021221">(May 09 2021 at 09:02)</a>:</h4>
<blockquote>
<p>#![feature(const_generics, const_evaluable_checked)]</p>
</blockquote>



<a name="238021238"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/Validating%20Trait%27s%20Associated%20Constants/near/238021238" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Boxy [she/her] <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/Validating.20Trait&#x27;s.20Associated.20Constants.html#238021238">(May 09 2021 at 09:02)</a>:</h4>
<p>the feature is <code>const_evaluatable_checked</code></p>



<a name="238021692"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/Validating%20Trait%27s%20Associated%20Constants/near/238021692" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/Validating.20Trait&#x27;s.20Associated.20Constants.html#238021692">(May 09 2021 at 09:08)</a>:</h4>
<p><span class="user-mention" data-user-id="326176">@Boxy [she/her]</span> Well that's embarrassing. <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span></p>



<a name="238021709"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/Validating%20Trait%27s%20Associated%20Constants/near/238021709" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Boxy [she/her] <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/Validating.20Trait&#x27;s.20Associated.20Constants.html#238021709">(May 09 2021 at 09:09)</a>:</h4>
<p>it normally gives an error saying you have the wrong feature <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span></p>



<a name="238021721"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/Validating%20Trait%27s%20Associated%20Constants/near/238021721" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Boxy [she/her] <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/Validating.20Trait&#x27;s.20Associated.20Constants.html#238021721">(May 09 2021 at 09:09)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#![allow(incomplete_features)]</span><span class="w"></span>
<span class="cp">#![feature(const_generics, const_evaluable_checked)]</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
</code></pre></div>
<div class="codehilite"><pre><span></span><code>error[E0635]: unknown feature `const_evaluable_checked`
 --&gt; src/main.rs:2:28
  |
2 | #![feature(const_generics, const_evaluable_checked)]
  |                            ^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error
</code></pre></div>



<a name="238021725"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/Validating%20Trait%27s%20Associated%20Constants/near/238021725" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Boxy [she/her] <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/Validating.20Trait&#x27;s.20Associated.20Constants.html#238021725">(May 09 2021 at 09:09)</a>:</h4>
<p>I dont know why it doesn't do this in your case <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span></p>



<a name="238021798"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/Validating%20Trait%27s%20Associated%20Constants/near/238021798" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/Validating.20Trait&#x27;s.20Associated.20Constants.html#238021798">(May 09 2021 at 09:10)</a>:</h4>
<p>Seems like if there's another error, that happens before it reports the unknown feature.</p>



<a name="238022399"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/Validating%20Trait%27s%20Associated%20Constants/near/238022399" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/Validating.20Trait&#x27;s.20Associated.20Constants.html#238022399">(May 09 2021 at 09:22)</a>:</h4>
<p><span class="user-mention" data-user-id="326176">@Boxy [she/her]</span> Filed an issue about it. <a href="https://github.com/rust-lang/rust/issues/85107">https://github.com/rust-lang/rust/issues/85107</a></p>
<p>Thanks for the help!</p>



<a name="238022428"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/Validating%20Trait%27s%20Associated%20Constants/near/238022428" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Boxy [she/her] <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/Validating.20Trait&#x27;s.20Associated.20Constants.html#238022428">(May 09 2021 at 09:23)</a>:</h4>
<p>glad you filed an issue because I intended to file one and then got distracted and forgot about this ever happening until just now <span aria-label="rofl" class="emoji emoji-1f923" role="img" title="rofl">:rofl:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>